<?php
use yii\bootstrap\ActiveForm;
use common\helps\tools;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>
    .dataTable>tbody>tr>td{
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th{
        vertical-align: middle;
        font-size: 0.8em;
        text-align: center;
    }
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    label{
        text-align: right;
    }
    #table_id_example th, td {
         white-space: nowrap;
     }
    #table_list th,td{ font-family: '微软雅黑'; font-size: small; text-align: center}

</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">

                    <?php if ($pur){ ?>
                        <div class="col-md-12" style="padding-bottom: 20px">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>采购单</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="orderNo" id="orderNo" readonly value="<?= $stock['purchaseOrderNo'] ?>">
                            </div>
                        </div>
                    <?php } ?>

                    <div class="col-md-12" style="padding-bottom: 20px">
                        <label class="col-sm-2 control-label"><span class="text-danger">*</span>仓库</label>
                        <div class="col-sm-4">
                            <select id="warehouseId" name="warehouseId" class="depart-input" style="width: 200px">
                                <option value="">选择仓库</option>
                                <?php foreach($warehouse as $value) { ?>
                                    <option value="<?= $value['id']?>" <?=($stock&&$stock['warehouseId']==$value['id'])?'selected':'' ?>><?= $value['warehouseName']?></option>
                                <?php }?>
                            </select>
                            <button type="button" class="btn btn-default" id="wh-confirm-btn" style="width:70px;">确认</button>&nbsp;&nbsp;
                        </div>
                    </div>
                    <div class="col-md-12" style="padding-bottom: 20px">
                        <label class="col-sm-2 control-label"><span class="text-danger">*</span>入库日期</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" name="expecteDate" id="expecteDate" value="<?=$stock? date('Y-m-d',strtotime($stock['expecteDate'])):''?>" readonly>
                        </div>
                        <label class="col-sm-2 control-label">备注</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" name="remark" id="remark" value="<?=$stock?$stock['remark']:''?>">
                        </div>
                    </div>

                    <div class="col-md-12">
                        <?php if (!$pur){ ?>
                            <button type="button" class="btn btn-primary" id="show-materiel-btn" style="width: 100px; margin-bottom: 5px">添加</button>
                        <?php } ?>

                        <table id="table_list" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th style="width: 8%">物料ID</th>
                                <th style="width: 10%">物料名称</th>
                                <th style="width: 8%">供应商</th>
                                <th style="width: 8%">所属仓库</th>
                                <th style="width: 8%">规格描述</th>
                                <th style="width: 8%">包装方式</th>
                                <th style="width: 7%">采购单位</th>
                                <th style="width: 7%">订货单位</th>
                                <th style="width: 7%">入库数量</th>
                                <th style="width: 7%">单价</th>
                                <th style="width: 7%">生产日期</th>
                                <th style="width: 7%">含税采购金额</th>
                                <th style="width: 7%">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if($stock && $stock['dtl']){ foreach ($stock['dtl'] as $dtl){ ?>
                                <tr data-code="<?= $dtl['materielId'] ?>">
                                    <td><?= $dtl['materielId'] ?></td>
                                    <td><?= $dtl['materielName'] ?></td>
                                    <td><?= $dtl['supplierName'] ?></td>
                                    <td><?= $dtl['warehouseName'] ?></td>
                                    <td><?= $dtl['specDesc'] ?></td>
                                    <td><?= $dtl['packDesc'] ?></td>
                                    <td><?= $dtl['purchaseUnit'] ?></td>
                                    <td><?= $dtl['orderUnit'] ?></td>
                                    <td><input name="amount" value="<?= $dtl['qty'] ?>" style="width: 80px"></td>
                                    <td><input readonly name="price" value="<?= $dtl['price'] ?>" style="width: 80px"></td>
                                    <td><input name="productDate" value="<?= $dtl['productDate'] ?>" style="width: 90px" readonly></td>
                                    <td><input name="totalPrice" value="<?= sprintf("%.4f",$dtl['price']*$dtl['qty']) ?>" style="width: 80px" readonly></td>
                                    <td><?php if($stock['stockStatus']=='0'){ ?><a name="del-materiel">删除</a><?php } ?></td>
                                </tr>
                            <?php } } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="box-footer" style="text-align: right">
                    <button type="button" class="btn btn-default" onclick="history.back();">返回
                    </button>
                    <button type="button" class="btn btn-primary hideBtn" onclick="saveReceipt()">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="add-materiel-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加物料</h4>
            </div>
            <div class="modal-body">
                <div class="box-header">
                    物料名称/编号: <input class="depart-input" id="searchKey" type="text">&nbsp;&nbsp;
                    供应商：<select id="supplierId" class="depart-input" style="width: 200px">
                        <option value="">请选择</option>
                        <?php foreach($supplier as $value) { ?>
                            <option value="<?= $value['id']?>"><?= $value['supplierName']?></option>
                        <?php }?>
                    </select>&nbsp;&nbsp;
                    <button type="button" class="btn btn-primary depart-modal" id="search-btn" style="width: 100px;">查询</button> &nbsp;
                </div>
                <div class="box-body">
                    <table id="search_list" class="display">
                        <thead>
                        <tr>
                            <th style="width: 13%">物料Id</th>
                            <th style="width: 15%">物料名称</th>
                            <th style="width: 10%">供应商</th>
                            <th style="width: 10%">所属仓库</th>
                            <th style="width: 10%">规格描述</th>
                            <th style="width: 10%">包装方式</th>
                            <th style="width: 11%">采购<br/>单位</th>
                            <th style="width: 11%">订货<br/>单位</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="add-materiel-btn">添加</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="select-purchase-modal" style="background: rgba(236, 230, 230, 0.3);" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">选择采购单</h4>
            </div>
            <div class="modal-body">
                <div class="box-header">
                    <div style="margin-bottom: 5px">
                        订单编号：<input class="depart-input" id="search_orderNo" style="width: 150px" type="text">
                        仓库：<select id="search_warehouseId" name="search_warehouseId" class="depart-input" style="width: 150px">
                            <option value="">选择仓库</option>
                            <?php foreach($warehouse as $value) { ?>
                                <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                            <?php }?>
                        </select>
                        采购时间：<input class="depart-input" id="search_purchaseDate" style="width: 200px" type="text">
                        <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#search_purchaseDate').val('')">清空</button>
                        物料名称：<input class="depart-input" id="search_materielName" style="width: 150px" type="text">
                        <button type="button" class="btn btn-primary depart-modal" id="select_button" style="width: 80px; margin-left: 41px">查询</button>
                        <button type="button" class="btn btn-warning depart-modal" id="reset_button" style="width: 80px;">重置</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="purchase_list" class="display">
                        <thead>
                        <tr>
                            <th style="width: 15%">采购单号</th>
                            <th style="width: 20%">供应商</th>
                            <th style="width: 20%">仓库</th>
                            <th style="width: 15%">采购日期</th>
                            <th style="width: 15%">预计送货日期</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="/static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>

<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script type="text/javascript" src="../static/app/stock/index.js?version=<?=Yii::$app->params['version']?>"></script>

<script>
    var tempData = {};
    var selectData = {};
    var searchDataTable = null;
    var searchPurchaseTable = null;
    var stockNo = '', dateFlg = 0;
    var purchaseOrderNo = '';
    var confirmFlg = 0;//确认仓库标记
    var orderConfirmFlg = 0;//确认采购单标记

    <?php if($stock){ ?>
    stockNo = '<?=$stock['stockNo']?>';
    purchaseOrderNo = '<?=$stock['purchaseOrderNo']?>';
    <?php if($stock['stockStatus']=='0'){ ?>
        <?php if($stock['dtl']){ foreach ($stock['dtl'] as $dtl){ ?>
        selectData['<?=$dtl['materielId']?>'] = {
            materielName:'<?=$dtl['materielName']?>',
            materielId:'<?=$dtl['materielId']?>',
            supplierId:'<?=$dtl['supplierId']?>',
            price: '<?=$dtl['price']?>',
            qty: '<?=$dtl['qty']?>',
            amount: '<?=$dtl['amount']?>',
            safeDay: '<?=$dtl['safeDay']?>',
            productDate: '<?=$dtl['productDate']?>'
        };
        confirmFlg = 1;
        $('#wh-confirm-btn').text('重置');
        $('#warehouseId').attr('disabled', 'true').css('color', '#ccc');
        $('#order-confirm-btn').text('重置');
        $('#orderNo').attr('disabled', 'true').css('color', '#ccc');

    <?php }}} else { ?>
        $('input').attr("disabled","disabled");
        $('#show-materiel-btn').hide();
        $('.hideBtn').hide();
        $('#wh-confirm-btn').hide();
        $('#order-confirm-btn').hide();

    <?php }} ?>


    <?php if ($pur){ ?>
    $('#wh-confirm-btn').hide();
    $('#warehouseId').attr('disabled', 'true').css('color', '#ccc');
    <?php } ?>


    <?php if ($pur && $stock['purchaseOrderNo']){ ?>
    $('#orderNo').attr('disabled', 'true').css('color', '#ccc');
    <?php } ?>


    $(function () {
        $('#expecteDate').datepicker({ language:'zh-CN', format:'yyyy-mm-dd'} );
        $(document).on('click', 'input[name=productDate]', function () {
            $(this).datepicker({ language:'zh-CN', format:'yyyy-mm-dd', showOn:'focus'} ).focus();
        });
    });

    //确认、重置仓库
    $("#wh-confirm-btn").on('click', function () {
        if ($(this).text() == '确认') {
            confirmFlg = 1;
            if ($('#warehouseId').val() == '') {
                $.jBox.tip('请选择仓库');
                return;
            }
            $('#warehouseId').attr('disabled', 'true').css('color', '#ccc');
            $(this).text('重置');
        } else {
            dateFlg = 1;
            confirmFlg = 0;
            selectData = {};
            tempData = {};
            $('#table_list tbody').html('');
            $('#warehouseId').removeAttr('disabled').css('color', '');
            $(this).text('确认');
        }
    });



    //选择采购单
    $('#search_purchaseDate').daterangepicker({
        language: 'zh-CN',
        timePicker: false,
        timePickerIncrement: 30,
        format: 'YYYY-MM-DD',
        locale: {
            format: 'YYYY-MM-DD',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        }
    });
    $('#search_purchaseDate').datepicker({ language:'zh-CN', format:'yyyy-mm-dd'} );


    searchPurchaseTable = $('#purchase_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/purchase-order/get-list',
            type:'post',
            data:function(d){
                d.orderNo = $('#search_orderNo').val();
                d.materielName = $('#search_materielName').val();
                d.warehouseId = $('#search_warehouseId').val();
                d.purchaseDate = $('#search_purchaseDate').val();
                d.orderStatus = 1;
            },
            error:getError
        },
        aoColumns: [
            { "data": "orderNo", "bSortable": false },
            { "data": "supplierName", "bSortable":false },
            { "data": "warehouseName", "bSortable":false },
            { "data": "purchaseDate", "bSortable":false },
            { "data": "pre_deliveryDate", "bSortable":false },
            { "data": "", "bSortable": false},
        ],
        columnDefs: [
            {
                "targets": [-1],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    html = '<a href="javascript:select_purchase(\''+full.orderNo+'\')">选择</a>';
                    return html;
                }
            }
        ]
    });


    $("#orderNo").click(function () {
        $('#select-purchase-modal').modal('show');
        $('#search_orderNo').val('');
        $('#search_materielName').val('');
        $('#search_purchaseDate').val('');
        $('#search_warehouseId').val('');
        searchPurchaseTable.ajax.reload();

    });
    $("#select_button").on('click', function () {
        searchPurchaseTable.ajax.reload();
    });
    //重置操作
    $('#reset_button').click(function(){
        $('#search_orderNo').val('');
        $('#search_materielName').val('');
        $('#search_purchaseDate').val('');
        $('#search_warehouseId').val('');
        searchPurchaseTable.ajax.reload();
    });


    function select_purchase(orderNo){
        $("#orderNo").val(orderNo);
        purchaseOrderNo = orderNo;
        orderConfirmFlg = 1;

        var params = {
            orderNo: orderNo
        };
        $.post('/purchase-order/get-detail', params,
            function (data) {
                $("#warehouseId").val(data.warehouseId);
                $("#remark").val(data.remark);
                $("#expecteDate").datepicker('setDate', data.pre_deliveryDate);


                $('#table_list tbody').html('');
                selectData = {};
                var html = '';
                $.each(data.dtl,function (i, v) {
                    if(!selectData[v['materielId']]){
                        html += '<tr data-code="'+ v['materielId'] +'">';
                        html += '   <td>'+ v['materielId'] +'</td>';
                        html += '   <td>'+ v['materielName'] +'</td>';
                        html += '   <td>'+ data.supplierName +'</td>';
                        html += '   <td>'+ data.warehouseName +'</td>';
                        html += '   <td>'+ v['specDesc'] +'</td>';
                        html += '   <td>'+ v['packDesc'] +'</td>';
                        html += '   <td>'+ v['purchaseUnit'] +'</td>';
                        html += '   <td>'+ v['orderUnit'] +'</td>';
                        html += '   <td><input type="text" name="amount" style="width:80px;" value="'+v['qty']+'"></td>';
                        html += '   <td><input type="text" readonly name="price" value="'+ v['referPrice'] +'" style="width:80px;"></td>';
                        html += '   <td><input type="text" name="productDate" style="width:90px;" readonly></td>';
                        html += '   <td><input type="text" name="totalPrice" style="width:80px;" readonly value="'+(v['referPrice'] * v['qty']).toFixed(4)+'"></td>';
                        html += '   <td><a name="del-materiel">删除</a></td>';
                        html += '</tr>';
                    }
                    v['supplierId'] = data.supplierId;
                    selectData[v['materielId']] = v;
                });
                dateFlg = 1;
                $('#table_list tbody').append(html);

                $('#select-purchase-modal').modal('hide');
            },
            'json'
        );


    }


    //初始化modal
    $('#show-materiel-btn').on('click', function () {

        if (!confirmFlg) {
            $.jBox.tip('请确认仓库');
            return;
        }

        $('#add-materiel-modal').modal('show');
        if(searchDataTable) {
            if(dateFlg){
                searchDataTable.ajax.reload();
            }
            return;
        }

        searchDataTable = $('#search_list').DataTable({
            dom: '<"top">rt<"bottom"flip><"clear">',
            language: {
                "sUrl":'../static/plugins/datatables/language.json'
            },
            "searching": false,
            "processing": true,
            "serverSide": true,
            ajax:{
                url:'/stock-tmp/get-materiel-list',
                type:'post',
                data:function(d){
                    d.warehouseId = $("#warehouseId").val();
                    d.supplierId = $("#supplierId").val();
                    d.searchKey = $('#searchKey').val();
                    d.storeFlg = 1;
                },
                error:getError
            },
            aoColumns: [
                { "data": "id", "bSortable": false },
                { "data": "materielName", "bSortable": false },
                { "data": "supplier", "bSortable": false },
                { "data": "warehouseName", "bSortable": false },
                { "data": "specDesc", "bSortable": false },
                { "data": "packDesc", "bSortable": false },
                { "data": "purchaseUnit", "bSortable": true },
                { "data": "orderUnit", "bSortable":false },
                { "data": "id", "bSortable":false },
            ],
            columnDefs: [
                {
                    "targets": [-1],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        if(!selectData[data]){
                            return '<input style="width:20px;height:20px;" type="checkbox" class="input-check" value="'+data+'" '+ (tempData[data]? 'checked' : '') +'>' +
                                '<input type="hidden" name="safeDay" value="'+full.safeDay+'">'+
                                '<input type="hidden" name="referPrice" value="'+full.referPrice+'">';
                        }
                        return '';
                    }
                },
                {
                    "targets": [2],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        var html = '';
                        if(data.length > 1){
                            html = '<select name="supplier">';
                            $.each(data, function(i,v){
                                html += '<option value="'+v.id+'">'+v.supplierName+'</option>';
                            });
                            html += '<select>';
                        }else{
                            html = '<input name="supplierId" type="hidden" value="'+data[0].id+'">';
                            html += '<span name="supplierName">'+data[0].supplierName+'</span>'
                        }
                        return html;
                    }
                }
            ],
        });
    });

    //modal 搜索按钮
    $('#search-btn').on('click', function () {

        if (searchDataTable) {
            searchDataTable.ajax.reload();
            return;
        }
    });

    //modal 选中/取消 checkbox
    $('#search_list').on('click', 'input:checkbox', function () {
        var code = $(this).val();
        if($(this).is(':checked')){
            var _tr = $(this).parents('tr'),
                supplierName = '',
                supplierId = _tr.find('input[name=supplierId]').val();
            if(supplierId){
                supplierName = _tr.find('span[name=supplierName]').html();
            }else{
                supplierId = _tr.find('select').val();
                supplierName = _tr.find('select option:selected').text();
            }
            tempData[code] = {
                materielId:_tr.find('td').eq(0).html(),
                materielName:_tr.find('td').eq(1).html(),
                supplierId:supplierId,
                supplierName:supplierName,
                warehouseName:_tr.find('td').eq(3).html(),
                specDesc:_tr.find('td').eq(4).html(),
                packDesc:_tr.find('td').eq(5).html(),
                purchaseUnit:_tr.find('td').eq(6).html(),
                orderUnit:_tr.find('td').eq(7).html(),
                safeDay:_tr.find('input[name=safeDay]').val(),
                referPrice:_tr.find('input[name=referPrice]').val(),
            }
        } else {
            delete tempData[code];
        }
    });

    //切换供应商
    $('#search_list').on('change', 'select', function () {
        var _tr = $(this).parents('tr'),
            code = _tr.find('input:checkbox').val();
        if(tempData[code]){
            tempData[code]['supplierId'] = _tr.find('select').val();
            tempData[code]['supplierName'] = _tr.find('select option:selected').text();
        }
    });

    //modal 添加按钮
    $('#add-materiel-btn').on('click',function () {
        if($.isEmptyObject(tempData)){
            $.jBox.tip('请选择物料', '错误');
        }else {
            var html = '';
            $.each(tempData,function (i, v) {
                if(!selectData[i]){
                    html += '<tr data-code="'+ i +'">';
                    html += '   <td>'+ v['materielId'] +'</td>';
                    html += '   <td>'+ v['materielName'] +'</td>';
                    html += '   <td>'+ v['supplierName'] +'</td>';
                    html += '   <td>'+ v['warehouseName'] +'</td>';
                    html += '   <td>'+ v['specDesc'] +'</td>';
                    html += '   <td>'+ v['packDesc'] +'</td>';
                    html += '   <td>'+ v['purchaseUnit'] +'</td>';
                    html += '   <td>'+ v['orderUnit'] +'</td>';
                    html += '   <td><input type="text" name="amount" style="width:80px;"></td>';
                    html += '   <td><input readonly type="text" name="price" value="'+ v['referPrice'] +'" style="width:80px;"></td>';
                    html += '   <td><input type="text" name="productDate" style="width:90px;" readonly></td>';
                    html += '   <td><input type="text" name="totalPrice" style="width:80px;" readonly></td>';
                    html += '   <td><a name="del-materiel">删除</a></td>';
                    html += '</tr>';
                }
                selectData[i] = v;
            });
            dateFlg = 1;
            tempData = {}; //清空临时数据
            $('#table_list tbody').append(html);
            $('#add-materiel-modal').modal('hide');
        }
    });

    //删除事件
    $(document).on('click','a[name=del-materiel]',function () {
        var _tr = $(this).parents('tr');
        delete selectData[_tr.attr('data-code')];
        _tr.remove();
    });


    $(document).on('change','input[name=amount]',function () {
        var _tr = $(this).parents('tr');
        var referPrice = _tr.find('input[name=price]').val();
        var qty = $(this).val();
        _tr.find('input[name=totalPrice]').val((referPrice * qty).toFixed(4));
    });



    //提交事件
    function saveReceipt() {
        if($("#warehouseId").val()==''){
            $.jBox.tip('请选择仓库');
            return;
        }
        if($("#expecteDate").val()==''){
            $.jBox.tip('请选择预计时间');
            return;
        }
        if($.isEmptyObject(selectData)){
            $.jBox.tip('请添加物料信息');
            return;
        }
        var errMsg = '';
        var trList = $("#table_list tbody").find('tr[data-code]');
        $.each(trList, function () {
            var code = $(this).attr('data-code');
            var amount = $(this).find('input[name=amount]').val();
            var price = $(this).find('input[name=price]').val();
            var productDate = $(this).find('input[name=productDate]').val();

            if($.isEmptyObject(selectData[code])) {
                errMsg = '数据异常，请刷新页面后重试';
                return false;
            }
            if($.trim(amount) == '') {
                errMsg = '请输入库数量';
                return false;
            }else if( !$.isNumeric(amount)) {
                errMsg = '请输入正确的入库数量';
                return false;
            }
            if($.trim(price) == ''){
                errMsg = '请输入单价';
                return false;
            }else if( !$.isNumeric(price)) {
                errMsg = '请输入正确的单价';
                return false;
            }
            if($.trim(productDate) == '') {
                errMsg = '请输入生产日期';
                return false;
            }
            selectData[code]['amount'] = amount;
            selectData[code]['price'] = price;
            selectData[code]['productDate'] = productDate;
        });

        if(errMsg){ $.jBox.tip(errMsg); return; }

        var stockData = {
            stockNo:stockNo,
            purchaseOrderNo:purchaseOrderNo,
            sType: '0',
            warehouseId: $("#warehouseId").val(),
            expecteDate: $("#expecteDate").val(),
            remark: $("#remark").val(),
            materiels:selectData
        }
        $.post('/stock-tmp/save-stock', stockData,
            function (data) {
                if(data.code == 200){
                    $.jBox.tip('保存成功', 'success');
                    window.setTimeout(function () {
                        history.back();
                    }, 1000);
                }else{
                    $.jBox.error(data.val, '错误');
                }
            },
            'json'
        );
    }

</script>